package com.chocus.core.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.chocus.common.utils.RequestUtils;
import com.chocus.core.service.buyer.SessionProvider;

public class BuyerInterceptor implements HandlerInterceptor {

	@Autowired
	private SessionProvider sessionProvider;

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object,
			Exception exception) throws Exception {

	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub

	}

	/**
	 * 登录前
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
		// 判断用户是否登录
		String username = sessionProvider.getAttribute(RequestUtils.getCSESSIONID(request, response));
		if (username == null) {
			response.sendRedirect("http://localhost:8082/login.aspx?returnUrl=" + request.getParameter("returnUrl"));
			return false;
		}
		return true;
	}

}
